package com.xuzao.developer.dao.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xuzao.developer.base.criteria.SysUserCriteria;
import com.xuzao.developer.base.model.SysUser;
import com.xuzao.developer.dao.SysUserDao;
import com.xuzao.developer.dto.SysUserDTO;
import com.xuzao.developer.mapper.SysUserMapper;
import com.xuzao.developer.tools.base.BaseServiceSupport;
import com.xuzao.developer.tools.entity.Pager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * @author xuzao
 * @description
 * @date 2019-08-22 11:37:49
 **/
@Service("sysUserDao")
public class SysUserDaoImpl extends BaseServiceSupport<SysUser> implements SysUserDao {

    @Resource
    private SysUserMapper sysUserMapper;


    @Override
    public PageInfo<SysUser> list(SysUserDTO dto, Pager pager) {
        SysUserCriteria criteria = this.assemblyQuerycond(dto);
        if (null != pager) {
            if (null != pager.getPageSize() && null != pager.getPageIndex()) {
                PageHelper.startPage(pager.getPageIndex(), pager.getPageSize());
            }
        }
        PageInfo<SysUser> pageInfo = new PageInfo<>(this.sysUserMapper.selectByExample(criteria));
        return pageInfo;
    }

    //通用查询方法条件组装
    public SysUserCriteria assemblyQuerycond(SysUserDTO dto) {
        SysUserCriteria criteria = new SysUserCriteria();
        SysUserCriteria.Criteria c = criteria.createCriteria();

        if (null != dto) {
            if (StringUtils.isNotBlank(dto.getPassWord())) {
                c.andPassWordEqualTo(dto.getPassWord());
            }
            if (StringUtils.isNotBlank(dto.getUserName())) {
                c.andUserNameEqualTo(dto.getUserName());
            }
            if (StringUtils.isNotBlank(dto.getRealName())){
                c.andRealNameLike("%"+dto.getRealName()+"%");
            }
            if (dto.getWorkType() != null){
                c.andWorkTypeEqualTo(dto.getWorkType());
            }

        }
        return criteria;
    }
}